﻿Imports System.ComponentModel
Imports Microsoft.SqlServer.Management.Smo
Imports BITC.DatabaseManagement
Imports System.Reflection

Public Class frmServer

#Region "Declaration"
    Private _isLoadedServer As Boolean = False
    Delegate Function GetControlValueCallback(ByVal oControl As Control, ByVal propName As String) As Object
#End Region

#Region "Constructor"

#End Region

#Region "Property"

#End Region

#Region "Method"

    ''' <summary>
    ''' Sinh dữ liệu cho combo Authentication
    ''' </summary>
    ''' <remarks>NDTRUNG - 10/2/2012</remarks>
    Private Sub PopulateAuthentication()
        Dim lstAuthentication As New List(Of AuthenticationMode)
        lstAuthentication.Add(New AuthenticationMode() With {.Id = 0, .AuthenticationName = "Windows Authentication"})
        lstAuthentication.Add(New AuthenticationMode() With {.Id = 1, .AuthenticationName = "SQL Server Authentication"})

        With cboAuthentication
            .DataSource = lstAuthentication
            .DisplayMember = "AuthenticationName"
            .ValueMember = "Id"
        End With
    End Sub

    ''' <summary>
    ''' Chuyển trạng thái cho TextBox Username và Password
    ''' </summary>
    ''' <param name="isEnable"></param>
    ''' <remarks>NDTRUNG - 13/2/2012</remarks>
    Private Sub SetUsernameAndPasswordStatus(ByVal isEnable As Boolean)
        txtUsername.Enabled = isEnable
        txtPassword.Enabled = isEnable
    End Sub

    ''' <summary>
    ''' Load danh sách các SQL Instance.
    ''' </summary>
    ''' <param name="isLocal">Load danh sách Server trong máy local, hoặc mạng nội bộ</param>
    ''' <remarks>NDTRUNG - 15/2/2012</remarks>
    Private Sub PopulateServer(ByVal isLocal As Boolean)
        cboServerName.DataSource = SmoApplication.EnumAvailableSqlServers(isLocal)
        cboServerName.ValueMember = "Name"
        cboServerName.DisplayMember = "Name"

    End Sub

    ''' <summary>
    ''' Dùng để lấy giá trị của thuộc tính tương ứng trong Control truyền vào
    ''' </summary>
    ''' <param name="oControl"></param>
    ''' <param name="propName"></param>
    ''' <returns></returns>
    ''' <remarks>NDTRUNG - 23/04/2012</remarks>
    Private Function GetControlValue(ByVal oControl As Control, ByVal propName As String)
        Dim oValue As Object = Nothing
        If oControl.InvokeRequired Then
            Dim d As New GetControlValueCallback(AddressOf GetControlValue)
            oValue = oControl.Invoke(d, New Object() {oControl, propName})
        Else
            Dim t As Type = oControl.[GetType]()
            Dim props As PropertyInfo() = t.GetProperties()
            For Each p As PropertyInfo In props
                If p.Name.ToUpper() = propName.ToUpper() Then
                    oValue = p.GetValue(oControl, Nothing)
                End If
            Next
        End If
        Return oValue
    End Function

#End Region

#Region "Event"

#Region "Form"

    Private Sub frmServer_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Try
            PopulateAuthentication()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

#Region "Button"

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnConnect.Click
        Try
            bgwServer.RunWorkerAsync()
            Me.Enabled = False
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnCancel.Click
        Try
            DialogResult = DialogResult.Cancel
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

#Region "Combo"

    Private Sub cboAuthentication_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cboAuthentication.SelectedIndexChanged
        Try
            SetUsernameAndPasswordStatus(cboAuthentication.SelectedIndex = 1)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub cboServerName_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboServerName.DropDown
        Try
            If Not _isLoadedServer Then
                PopulateServer(True)
                _isLoadedServer = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub cboServerName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboServerName.TextChanged
        Try
            btnConnect.Enabled = Not String.IsNullOrEmpty(cboServerName.Text)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

#End Region

#Region "Background Worker"

    Private Sub bgwServer_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles bgwServer.DoWork
        SMOHelper.Connect(GetControlValue(cboServerName, "Text"), GetControlValue(txtUsername, "Text"), GetControlValue(txtPassword, "Text"), GetControlValue(cboAuthentication, "SelectedIndex") = 0)

        If bgwServer.CancellationPending Then
            e.Cancel = True
            Return
        End If
    End Sub

    Private Sub bgwServer_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bgwServer.RunWorkerCompleted
        Try
            If e.Error IsNot Nothing OrElse e.Cancelled Then
                MessageBox.Show(e.Error.Message)
            Else
                DialogResult = DialogResult.OK
            End If
            Me.Enabled = True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

#End Region


End Class